package com.code.aspectj;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.code.aspectj.util.CheckHandler;
import com.code.aspectj.util.Interceptor;
import com.code.aspectj.util.PermissionUtils;
import com.code.aspectj.util.SafeHandler;

import java.lang.ref.WeakReference;


/**
 * Created by RLZ
 * on 2021/12/6
 * 事件拦截器
 */
public class AppInterceptor {

    public static final String TAG = "aspect_log";

    public static final String TIPS = "请检查网络连接!";

    private static Interceptor sInterceptor;

    private static WeakReference<Context> sContextWeakReference;

    private static PermissionUtils.OnPermissionDeniedListener sOnPermissionDeniedListener;

    private static SafeHandler sSafeHandler;

    private static CheckHandler sCheckHandler;

    public static void init(Context app) {
        sContextWeakReference = new WeakReference<>(app.getApplicationContext());
    }

    public static Context getContext() {
        if (sContextWeakReference == null || sContextWeakReference.get() == null)
            throw new NullPointerException("AppInterceptor 未初始化");
        return sContextWeakReference.get();
    }

    @Nullable
    public static CheckHandler getCheckHandler() {
        return sCheckHandler;
    }

    public static void setCheckHandler(CheckHandler checkHandler) {
        sCheckHandler = checkHandler;
    }

    @Nullable
    public static Interceptor getInterceptor() {
        return sInterceptor;
    }

    public static void setInterceptor(@NonNull Interceptor interceptor) {
        sInterceptor = interceptor;
    }

    public static void setOnPermissionDeniedListener(@NonNull PermissionUtils.OnPermissionDeniedListener listener) {
        sOnPermissionDeniedListener = listener;
    }

    @Nullable
    public static PermissionUtils.OnPermissionDeniedListener getOnPermissionDeniedListener() {
        return sOnPermissionDeniedListener;
    }

    public static void setSafeHandler(SafeHandler handler) {
        sSafeHandler = handler;
    }

    @Nullable
    public static SafeHandler getSafeHandler() {
        return sSafeHandler;
    }
}
